var id = 1;
// var $ =layui.jquery;

function add() {
	var name = $("#inname").val();
	var sex = $("#insex").val();
	var hobby1 = $("#inhobby1").prop("checked");
	var hobby2 = $("#inhobby2").prop("checked");
	var hobby3 = $("#inhobby3").prop("checked");
	var hobby="";
	// console.log(hobby1);
	if(hobby1==true){
		hobby+=$("#inhobby1").val()+" ";
	}
	if(hobby2==true){
		hobby+=$("#inhobby2").val()+" ";
	}
	if(hobby3==true){
		hobby+=$("#inhobby3").val();
	}
	// console.log(hobby);
	var address = $("#inaddress").val();
	// console.log(name);
	var html = "" +
		"<tr id='tr" + id + "'><td>" + id + "</td>" +
		"<td id='td2" + id + "'>" + name + "</td>" +
		"<td id='td3" + id + "'>" + sex + "</td>" +
		"<td id='td4" + id + "'>" + hobby + "</td>" +
		"<td id='td5" + id + "'>" + address + "</td>" +
		"<td id='td6" + id + "'><a href='javascript:del(" + id + ");'>删除</a> " +
		"<a href='javascript:upd(" + id + ");'>修改</a>" +
		"</td></tr>"
	$("#tbody").append(html);
	id++;
}

function del(idd) {
	var tr = $("#tr" + idd);
	if (confirm("确实删除？")) {
		tr.remove();
	}
}

function upd(idd) {
	var td2 = $("#td2" + idd);
	var td3 = $("#td3" + idd);
	var td4 = $("#td4" + idd);
	var td5 = $("#td5" + idd);
	var td6 = $("#td6" + idd);
	// console.log(td2);
	// console.log(td2.text());
	td2.html("<input type='text' id='tdname" + idd + "' value='" + td2.text() + "' />");
	td3.html("<input type='text' id='tdsex" + idd + "' value='" + td3.text() + "' />");
	td4.html("<input type='text' id='tdhobby" + idd + "' value='" + td4.text() + "' />");
	td5.html("<input type='text' id='tdaddress" + idd + "' value='" + td5.text() + "' />");
	td6.html("<a id='tdaction" + idd + "' href='javascript:save(" + idd + ");'>保存</a>");
}

function save(idd) {
	var td2 = $("#tdname" + idd);
	var td3 = $("#tdsex" + idd);
	var td4 = $("#tdhobby" + idd);
	var td5 = $("#tdaddress" + idd);
	var td6 = $("#tdaction" + idd);

	$("#td2" + idd).html(td2.val());
	$("#td3" + idd).html(td3.val());
	$("#td4" + idd).html(td4.val());
	$("#td5" + idd).html(td5.val());
	$("#td6" + idd).html("<a href='javascript:del(" + idd + ");'>删除</a> " +
		"<a href='javascript:upd(" + idd + ");'>修改</a>");
}

function search() {
	$("tr").css("background", "");
	
	var name = $("#name").val();
	var address = $("#address").val();
	if (name == "") return;
	
	
	for(var i=1;i<id;i++){
		var td1=$("#td2"+i).text();
		var td2=$("#td5"+i).text();
		if(td1.indexOf(name)>-1){
			if(address=="全部"){
				$("#tr"+i).css("background", "red");
			}
			else{
				if(td2==address){
					$("#tr"+i).css("background", "red");
				}
			}
		}
		
	}

}